package com.yssoft.controller.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.yssoft.controller.BaseController;
import com.yssoft.entity.core.TCode;
import com.yssoft.entity.core.TProcessingTechnology;
import com.yssoft.service.core.CodeService;
import com.yssoft.service.handle.ProcessService;

/**
 * @author 戈超
 * @time 2018/5/23 下午 18:37
 * @  检验管理控制器
 */
@Controller
@RequestMapping(value = "/process")
public class ProcessController  extends BaseController{

	@Resource
	private ProcessService processService;
	@Resource
	private CodeService codeService;
	
	/**
	 * 跳转工艺管理列表
	 * @author 戈超
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "processList")
	public String processList(Model model){
		return "handle/processList";
	}
	
	/**
	 * 查询工艺管理信息
	 * @author 戈超
	 * @param page
	 * @param limit
	 * @param processName
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "queryProcessList")
	public JSONObject queryProcessList(@RequestParam(name = "page",defaultValue = "0") int page,
    		@RequestParam(name = "limit",defaultValue = "10") int limit,String processName, Model model){
		JSONObject pageList = processService.queryProcessList(page, limit, processName);
		return pageList;
	}
	
	/**
	 * 跳转工艺管理新增页
	 * @author 戈超
	 * @return
	 */
	@RequestMapping(value = "processAdd")
	public String processAdd(Integer processId, Model model){
		if(processId != null){
			TProcessingTechnology proess = processService.queryProcessById(processId);
			model.addAttribute("proess", proess);
		}
		return "handle/processadd";
	}
	
	/**
	 * 创建工艺管理
	 * @author 戈超
	 * @param tProcessingTechnology
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "processAddSave")
	public String processAddSave(TProcessingTechnology tProcessingTechnology, RedirectAttributes redirectAttributes){
		try {
			processService.processAddSave(tProcessingTechnology);
			redirectAttributes.addFlashAttribute("message", "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "保存失败!");
		}
		return "redirect:processList";
	}
	
	/**
	 * 查看工艺指标
	 * @param inspectId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "queryProcessView")
	public String queryProcessView(Integer processId, Model model){
		TProcessingTechnology proess = processService.queryProcessView(processId);
		model.addAttribute("proess", proess);
		return "handle/processView";
	}
	
	/**
	 * 编辑工艺指标
	 * @author 戈超
	 * @param processId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "processEdit")
	public String processEdit(Integer processId, Model model){
		TProcessingTechnology proess = processService.queryProcessView(processId);
		model.addAttribute("proess", proess);
		Map<String, Object> param = new HashMap<>();
		param.put("classify", "gatherType");
		List<TCode> gatherType = codeService.findCodeByParam(param);
		model.addAttribute("gatherTypeList", gatherType);
		param.put("classify", "gatherMethod");
		List<TCode> gatherMethod = codeService.findCodeByParam(param);
		model.addAttribute("gatherMethod", gatherMethod);
		return "handle/processEdit";
	}
	
	@RequestMapping(value = "processEditSave")
	public String processEditSave(Integer processId, String delIds, String[] technicId, String[] code, String[] described,
			String[] gatherMethod, String[] name, String[] shareStat, String[] unit, String[] isInput, String[] quotaType, Model model, RedirectAttributes redirectAttributes){
		try {
			processService.processEditSave(processId, delIds, technicId, code, described, gatherMethod, name, shareStat, unit, isInput, quotaType);
			redirectAttributes.addFlashAttribute("message", "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "保存失败!");
		}
		return queryProcessView(processId, model);
	}
	
	/**
	 * 实时删除工艺指标
	 * @param delId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deleteTechnic")
	public String deleteTechnic(Integer delId){
		try {
			processService.deleteTechnic(delId);
			return "0";
		} catch (Exception e) {
			return "1";
		}
	}
	
	/**
	 * 工艺管理信息删除
	 * @author 戈超
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delProcess")
	public String delProcess(String ids){
		String result = "";
		String[] processIdArray = ids.split(",");
		try {
            for (int i = 0; i < processIdArray.length; i++) {
            	processService.delProcess(processIdArray[i]);
            }
            result = "success";
        } catch (Exception e) {
            e.printStackTrace();
            result = "error";
        }
		return result;
	}
	
	/**
	 * @方法名称: checkCode
	 * @功能描述: 校验code是否重复
	 * @作者: 洪丰
	 * @创建时间:2018年7月16日 下午7:46:14
	 * @param checkCode
	 * @return String
	 */
	@RequestMapping(value = "checkCode", method = RequestMethod.POST)
	@ResponseBody
	public String checkCode(@RequestParam(value = "checkCode") String checkCode) {
		String result = "1";
		TProcessingTechnology pt = new TProcessingTechnology();
		pt.setCode(checkCode);
		int resultInt = processService.queryPTByRecord(pt);
		if(resultInt > 0){
			result = "0";
		}
        return result;
	}
}
